package com.ruoyi.mateProcurement.service.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateTest;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateDevice.domain.MateDeviceModel;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateProcurement.domain.MateOutbound;
import com.ruoyi.mateProcurement.domain.MateProcurement;
import com.ruoyi.mateProcurement.domain.MateProcurementModel;
import com.ruoyi.mateProcurement.mapper.MateOutboundMapper;
import com.ruoyi.mateProcurement.mapper.MateProcurementMapper;
import com.ruoyi.mateProcurement.service.MateProcurementService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.material.domain.MateProductCheck;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.mapper.CheckReportMapper;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.paramters.domain.Parameter;
import com.ruoyi.paramters.mapper.ParameterMapper;
import com.ruoyi.product.domain.Product;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.mapper.ReadyMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateProcurementServiceImpl implements MateProcurementService {
    @Autowired
    MateProcurementMapper mateProcurementMapper;

    @Autowired
    ParameterMapper parameterMapper;

    @Autowired
    AuditMapper auditMapper;

    @Autowired
    MateWarehousingMapper warehousingMapper;

    @Autowired
    MateOutboundMapper mateOutboundMapper;

    @Autowired
    WarehousingRecordsMapper warehousingRecordsMapper;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    CheckReportMapper checkReportMapper;

    @Autowired
    ReadyMapper readyMapper;

    @Override
    public MateProcurement selectMateProcurement(Map<String, Object> resMap) {
        return mateProcurementMapper.selectMateProcurement(resMap);
    }

    @Override
    public String orderNum(Date date, int i) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //获取当前登录的用户
        Long companyId = sysUser.getCompanyId();
        if (companyId == null) {
            return "0";
        }
        //定义订单前缀，获取订单的前缀
        String matePre = "";
        Parameter parameter = parameterMapper.selectParameterById(companyId);
        String num = "";
        if (parameter == null) {
            return "0";
        }
        //i 生成订单编号的类型
        if (i == 0) {
            //生成原材料入库订单编号
            matePre = parameter.getMatePre();
            //查询采购订单
            MateProcurement mateProcurement = new MateProcurement();
            Map<String, Object> map = new HashMap<>();
            map.put("date", date);
            map.put("deptId", sysUser.getCompanyId());
//            map.put("deptId", 101L);
            if (mateProcurementMapper.selectPurchaseOrderNumList(map).size() != 0) {
                mateProcurement = mateProcurementMapper.selectPurchaseOrderNumList(map).get(0);
            }
            if (mateProcurement.getMateOrdernum() == null) {
                int count = 1;
                num = "00" + count;
            } else {
                String mateOrdernum = mateProcurement.getMateOrdernum();
                String substring = mateOrdernum.substring(mateOrdernum.length() - 3);
                int count = Integer.parseInt(substring);
                if (count + 1 < 10) {
                    num = "00" + (count + 1);
                } else if (10 <= (count + 1) && (count + 1) < 100) {
                    num = "0" + (count + 1);
                } else {
                    num = "" + (count + 1);
                }
            }
        } else if (i == 1) {
            matePre = parameter.getMateOutpre();
            MateOutbound mateOutbound = new MateOutbound();
            Map<String, Object> map = new HashMap<>();
            map.put("date", date);
            map.put("deptId", sysUser.getCompanyId());
            if (mateOutboundMapper.selectPurchaseOrderNumList(map).size() != 0) {
                mateOutbound = mateOutboundMapper.selectPurchaseOrderNumList(map).get(0);
            }
            if (mateOutbound.getMateGoodsNum() == null) {
                int count = 1;
                num = "00" + count;
            } else {
                String mateGoodsNum = mateOutbound.getMateGoodsNum();
                String substring = mateGoodsNum.substring(mateGoodsNum.length() - 3);
                int count = Integer.parseInt(substring);
                if (count + 1 < 10) {
                    num = "00" + (count + 1);
                } else if (10 <= (count + 1) && (count + 1) < 100) {
                    num = "0" + (count + 1);
                } else {
                    num = "" + (count + 1);
                }
            }

        }
        //获取输入框日期
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
//        String localDate = simpleDateFormat.format(date);
        SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyMMdd");
        String localDate = simpleDateFormats.format(date);
        String s = String.valueOf(new Date().getTime());
        String substring = s.substring(s.length() - 3);
        String orderNum = matePre + "-" + localDate + substring + "-" + num;
//        String orderNum = purPre + "-" + localDate + "-" + num;
        return orderNum;
    }

    @Override
    public int insertMatrProcurement(MateProcurement mateProcurement) {
        mateProcurement.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        int i = 0;
        Long readyId = mateProcurement.getMateProcurementModels().get(0).getReadyId();
        if (mateProcurement.getMateProcurementModels() != null) {
            try {
                mateProcurement.setReadyId(readyId);
                i = mateProcurementMapper.insertMateProcurement(mateProcurement);
                for (MateProcurementModel mateProcurementModel : mateProcurement.getMateProcurementModels()) {
                    mateProcurementModel.setDeptId(SecurityUtils.getLoginUser().getDeptId());
                    mateProcurementModel.setMateProcurementId(mateProcurement.getMateProcurementId());
                    mateProcurementMapper.insertMateProcurementModel(mateProcurementModel);
                    MateProductCheck mateProductCheck = new MateProductCheck();
                    mateProductCheck.setMateCheckId(mateProcurementModel.getMateCheckId());
                    mateProductCheck.setIsMtWarehousing("1");
                    checkReportMapper.updateMateProductCheck(mateProductCheck);
                }
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setReadyId(readyId);
                readyProcurement.setIsProcurement("1");
                readyMapper.updateReadyProcurement(readyProcurement);
            } catch (Exception e) {
                mateProcurementMapper.deleteMateProcurementById(mateProcurement.getMateProcurementId());
                mateProcurementMapper.removedeleteProcurementModelById(mateProcurement.getMateProcurementId());
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setReadyId(readyId);
                readyProcurement.setIsProcurement("0");
                readyMapper.updateReadyProcurement(readyProcurement);
            }
        }

        if (i > 0) {
            Audit audit = new Audit();
            audit.setOrderId(mateProcurement.getMateProcurementId());
            audit.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
            audit.setOrderType("8");
            audit.setAuditStatus("0");
            audit.setUserId(mateProcurement.getUserId());
            audit.setTaxAmount(mateProcurement.getTaxAmount());
            //订单编号
            audit.setOrderNum(mateProcurement.getMateOrdernum());
            auditMapper.insertAudit(audit);
            return i;
        } else {
            return 0;
        }
    }

    @Override
    public List<MaterialProduct> selectProductLists(MaterialProduct materialProduct) {
        return mateProcurementMapper.selectProductLists(materialProduct);
    }

    @Override
    public List<MateProcurement> selectMateProcurementList(MateProcurement mateProcurement) {
        return mateProcurementMapper.selectMateProcurementList(mateProcurement);
    }

    @Override
    public void deleteProcurement(Long mateProcurementId) {
        MateProductCheck mateProductCheck = new MateProductCheck();
        List<MateProcurementModel> list = mateProcurementMapper.selectMateProcurementModel(mateProcurementId);
        MateProcurement mateProcurement = mateProcurementMapper.selectMateProcurementByOrderId(mateProcurementId);
        for (MateProcurementModel mateProcurementModel : list) {
            mateProductCheck.setMateCheckId(mateProcurementModel.getMateCheckId());
            mateProductCheck.setIsMtWarehousing("0");
            checkReportMapper.updateMateProductCheck(mateProductCheck);
        }
        ReadyProcurement readyProcurement = new ReadyProcurement();
        readyProcurement.setReadyId(mateProcurement.getReadyId());
        readyProcurement.setIsProcurement("0");
        readyMapper.updateReadyProcurement(readyProcurement);
        mateProcurementMapper.deleteProcurement(mateProcurementId);
        HashMap hashMap = new HashMap();
        hashMap.put("orderId", mateProcurementId);
        hashMap.put("orderType", 8);
        auditMapper.deleteAuditWith(hashMap);
        mateProcurementMapper.removedeleteProcurementModelById(mateProcurementId);
    }

    @Override
    public int updateProcurement(MateProcurement mateProcurement) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long mateProcurementId = mateProcurement.getMateProcurementId();
        List<Long> longs = mateProcurementMapper.selectProcurementWarehousingIdList(mateProcurementId);
        Audit audit = new Audit();
        audit.setOrderId(mateProcurement.getMateProcurementId());
        audit.setCompanyId(sysUser.getCompanyId());
        audit.setOrderType("8");
        audit.setAuditStatus("0");
        audit.setUserId(mateProcurement.getUserId());
        audit.setTaxAmount(mateProcurement.getTaxAmount());
        auditMapper.updateAuditOther(audit);
        if (mateProcurement.getMateProcurementModels() != null) {
            List<Long> longs1 = new ArrayList<>();
            for (MateProcurementModel mateProcurementModel : mateProcurement.getMateProcurementModels()) {
                if (mateProcurementModel.getProcurementNo() != null) {
                    Long mateProcurementModelProcurementNo = mateProcurementModel.getProcurementNo();
                    longs1.add(mateProcurementModelProcurementNo);
                    mateProcurementMapper.updateProcurementModel(mateProcurementModel);
                } else {
                    mateProcurementModel.setMateProcurementId(mateProcurement.getMateProcurementId());
                    mateProcurementModel.setDeptId(sysUser.getCompanyId());
                    mateProcurementMapper.insertMateProcurementModel(mateProcurementModel);
                }
            }

            if (longs1.size() != 0 && longs != null) {
                //数组中剩余的数就是需要删除的数据
                boolean b = longs.removeAll(longs1);
                if (longs.size() != 0) {
                    for (int i = 0; i < longs.size(); i++) {
                        mateProcurementMapper.deleteProcurementModel(longs.get(i));
                    }
                }
            }
        } else {
            if (longs != null) {
                for (int i = 0; i < longs.size(); i++) {
                    mateProcurementMapper.deleteProcurementModel(longs.get(i));
                }
            }
        }
        mateProcurementMapper.updateProcurement(mateProcurement);
        return 1;
    }

    @Override
    public MateProcurement selectMateProcurementBy(MateProcurement mateProcurement) {
        return mateProcurementMapper.selectMateProcurementBy(mateProcurement);
    }

    @Override
    public MateProcurement selectMateProcurementByOrderId(Long mateProcurementId) {
        return mateProcurementMapper.selectMateProcurementByOrderId(mateProcurementId);
    }


    @Override
    public int caculation(MateProcurement mateProcurement) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        MateProcurement mateProcurement1 = mateProcurementMapper.selectMateProcurementBy(mateProcurement);
        if (mateProcurement1.getMateProcurementModels() != null) {
            List<MateProcurementModel> mateProcurementModels = mateProcurement1.getMateProcurementModels();
            WarehousingRecords warehousingRecords = new WarehousingRecords();
            for (MateProcurementModel mateProcurementModel : mateProcurementModels) {
                Long deptId = sysUser.getDeptId();
                String productType = mateProcurementModel.getMateProductType();
                Long productId = mateProcurementModel.getMateproductId();
                String batch = mateProcurementModel.getMateBatch();
                int num = Math.toIntExact(mateProcurementModel.getRealityNum());
                MateWarehousing warehousing = new MateWarehousing();
                warehousing.setDeptId(deptId);
                warehousing.setMateBatch(batch);
                warehousing.setMateProductType(productType);
                warehousing.setMateproductId(productId);
                warehousing.setWarehousingNum(num);
                warehousing.setMaterialId(mateProcurementModel.getMaterialId());
                warehousing.setEnterNum(mateProcurementModel.getEnterNum());
                MateWarehousing warehousing1 = warehousingMapper.selectMateWarehousing(warehousing);
                warehousingRecords.setInventoryNum(mateProcurementModel.getAllNum());
                warehousingRecords.setAllnum(Math.toIntExact(mateProcurementModel.getRealityNum()));
                if (warehousing1 == null) {
                    warehousingMapper.insertMateWarehousing(warehousing);
                } else {
                    int warehousingNum = warehousing1.getWarehousingNum();
                    warehousingNum = num + warehousingNum;
                    warehousing.setWarehousingNum(warehousingNum);
                    warehousing.setWarehousingId(warehousing1.getWarehousingId());
                    warehousingMapper.updateMateWarehousing(warehousing);
                }
                Long warehousingId = warehousing.getWarehousingId();
                warehousingRecords.setOrderNum(mateProcurement1.getMateOrdernum());
                warehousingRecords.setWarehousingId(warehousingId);
                warehousingRecords.setInventoryStatus("8");
                warehousingRecords.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                warehousingRecordsMapper.insertWarehousingRecords(warehousingRecords);
            }
        }
        return 1;
    }

    @Override
    public List<MateProcurement> selectProcurement(Long deptId) {
        return mateProcurementMapper.selectProcurement(deptId);
    }


}
